﻿using AutoMapper;
using MediatR;
using WMS.API.Application.Command.BasicManagement.ShelfManage;
using WMS.Domain.Entities.BasicManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.BasicManagement.ShelfManage
{
    /// <summary>
    /// 修改货架命令处理
    /// </summary>
    public class UpdShelfManageModelCommandHandler : IRequestHandler<UpdShelfManageModelCommand, APIResult<int>>
    {
        /// <summary>
        /// 货架管理仓储
        /// </summary>
        private readonly IBaseRepository<ShelfManageModel> _shelfManageModelBaseRepository;
        private readonly IMapper mapper;

        public UpdShelfManageModelCommandHandler(IBaseRepository<ShelfManageModel> shelfManageModelBaseRepository, IMapper mapper)
        {
            _shelfManageModelBaseRepository = shelfManageModelBaseRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 修改货架信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(UpdShelfManageModelCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //var model =await _shelfManageModelBaseRepository.FindAsync(request.Id);
                //var modelNew=mapper.Map(request,model);
                var model = mapper.Map<ShelfManageModel>(request);
                var i = await _shelfManageModelBaseRepository.UpdateAsync(model);
                return await Task.FromResult(i > 0 ? new APIResult<int>("货架信息修改成功") { Code = ResultCode.Success } : new APIResult<int>("货架信息修改失败") { Code = ResultCode.Failed });
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
